#!/bin/bash
conf_file='/root/scripts/conf/tunel_praca_windows_conf.txt'
log_file='/root/scripts/logs/tunel_praca_windows.log'
log_file_error='/root/scripts/logs/tunel_praca_windows.error.log'

max_count=1048576
last_count="`cat \"$conf_file\"`"
current_count="`cat \"$log_file\" | wc -l`"

echo $last_count
echo $current_count
echo "$current_count" > "$conf_file"

if [ "$last_count" -ge "$current_count" ]; then

	for pid in `ps aux | grep 'ssh marcin@windows' | grep 'localhost:1022:localhost:22' | grep 'localhost:2389:localhost:3389' | sed 's/root\ *//' | cut -d' ' -f1`; do
		echo $pid
		kill -9 $pid
	done

        rm "$log_file"
        touch "$log_file"
        current_count=0
        echo "$current_count" > "$conf_file"

	nohup ssh marcin@windows -L localhost:1022:localhost:22 -L localhost:2389:localhost:3389 "/bin/sh -c 'while true; do sleep 15; echo +; done'" 1>>"$log_file" 2>>"$log_file_error" &

fi

if [ "$current_count" -ge "$max_count" ]; then
	rm "$log_file"
	touch "$log_file"
	current_count=0
	echo "$current_count" > "$conf_file"
fi

